/**
 * Created by geo on 17/4/19.
 */
//使用列表实现
function Sale(price){
    this.price = price>0 ? price : 100;
    this.decorators_list = [];//z装饰者列表
}

//装饰池
Sale.prototype.decorators = {};
Sale.prototype.decorators.fedtax = {
    getPrice: (price)=>{
        return price + price*5/100;
    }
};
Sale.prototype.decorators.money = {
    getPrice: (price)=>{
        return "$"+price.toFixed(2);
    }
};
//装饰方法
Sale.prototype.decorate = function (decorator) {
    this.decorators_list.push(decorator);
};
//最终的处理
Sale.prototype.getPrice = function () {
    var price = this.price;
    for(let i=0;i<this.decorators_list.length;i++){
        let decorator = this.decorators_list[i];
        price = this.decorators[decorator].getPrice(price);
    }
    return price;
};

/////////////////test//////////////////
var sale = new Sale(100);
sale.decorate("fedtax");
sale.decorate("money");
var price = sale.getPrice();
console.log(price);


//使用AOP实现(面向切面编程)
Function.prototype.after = function (afterFn) {
    var self = this;
    return function () {
        var res1 = self.apply(this,arguments);
        return afterFn.call(this,res1);
    }
};

function Salee() {
    this.price = price>0 ? price : 100;
    this.getPrice = function () {
        return this.price;
    };
}
//装饰池
Salee.prototype.decorators = {};
const fedtax = function(price){
    return price + price*5/100;
};
const money = function(price){
    return "$"+price.toFixed(2);
};

var sale = new Salee(100);
sale.getPrice = sale.getPrice
                    .after(fedtax)
                    .after(money);

console.log(sale.getPrice());

console.log(0===false);